package com.weiyi.portal.modules.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weiyi.portal.dto.AdvertiseNewsDTO;
import com.weiyi.portal.modules.sms.model.SmsAdvertiseNewsRelation;
import com.weiyi.portal.modules.sms.mapper.SmsAdvertiseNewsRelationMapper;
import com.weiyi.portal.modules.sms.service.SmsAdvertiseNewsRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weiyi.portal.modules.ums.model.UmsNews;
import com.weiyi.portal.modules.ums.service.UmsNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PZC
 * @since 2022-11-08
 */
@Service
public class SmsAdvertiseNewsRelationServiceImpl extends ServiceImpl<SmsAdvertiseNewsRelationMapper, SmsAdvertiseNewsRelation> implements SmsAdvertiseNewsRelationService {
    @Autowired
    SmsAdvertiseNewsRelationService advertiseNewsRelationService;
    @Autowired
    UmsNewsService newsService;

    @Override
    public List<AdvertiseNewsDTO> getNewsByAid(Long aid) {
        QueryWrapper<SmsAdvertiseNewsRelation>queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(SmsAdvertiseNewsRelation::getAdvertiseId, aid);
        List<SmsAdvertiseNewsRelation> relations = advertiseNewsRelationService.list(queryWrapper);

        QueryWrapper<UmsNews>queryWrapper1=new QueryWrapper<>();
        queryWrapper.groupBy("type");
        List<UmsNews> umsNews = newsService.list(queryWrapper1);
        queryWrapper1.clear();
        List<AdvertiseNewsDTO>advertiseNewsDTOS=new ArrayList<>();

        for(UmsNews n:umsNews){
            queryWrapper1.lambda().eq(UmsNews::getType, n.getType());
            List<UmsNews> news = newsService.list(queryWrapper1);
            queryWrapper1.clear();

            AdvertiseNewsDTO advertiseNewsDTO=new AdvertiseNewsDTO();
            advertiseNewsDTO.setNews(news);
            advertiseNewsDTO.setName(n.getType());
            advertiseNewsDTOS.add(advertiseNewsDTO);
        }
        return advertiseNewsDTOS;
    }
}
